// 权限处理模块
const db = require('../db/index')
const util = require('util');
const query = util.promisify(db.query).bind(db)

function getTree(results, keyId) {
    // 生成一次菜单
    let arr = []
    for (idx in results) {
        if (results[idx].ps_level == 0) {
            if (keyId.length !== 0 && keyId.indexOf(results[idx].ps_id + '') == -1) {
                continue;
            }
            arr.push({
                id: result.ps_id,
                authName: result.ps_name,
                pid: result.ps_pis,
                path: result.ps_api_path,
                children: []
            })
        }
    }
    // console.log(arr);
    // 生成二级菜单
    for (idx in results) {
        if (results[idx].ps_level == 1) {
            for (var j = 0; j < arr.length; j++) {
                if (results[idx]['ps_pid'] == arr[j].id) {
                    if (keyId.length !== 0 && keyId.indexOf(results[idx].ps_id + '') == -1) {
                        continue;
                    }
                    arr[j].children.push({
                        id: results[idx].ps_id,
                        authName: results[idx].ps_name,
                        path: results[idx].ps_api_path,
                        pid: results[idx].ps_pid,
                        children: []
                    })
                }
            }
        }
    }
    // 生成三级菜单
    for (idx in results) {
        if (results[idx].ps_level == 2) {
            for (var j = 0; j < arr.length; j++) { //找一级菜单
                var secondMenu = arr[j].children //二级菜单
                for (var i = 0; i < secondMenu.length; i++) { //找出二级菜单里的每一项
                    if (results[idx]['ps_pid'] == secondMenu[i].id) {
                        if (keyId.length !== 0 && keyId.indexOf(results[idx].ps_id + '') == -1) {
                            continue;
                        }
                        secondMenu[i].children.push({
                            id: results[idx].ps_id,
                            authName: results[idx].ps_name,
                            path: results[idx].ps_api_path,
                            pid: results[idx].ps_pid + ',' + arr[j].id,
                        })
                    }
                }
            }
        }
    }
}
exports.rightList = (req, res) => {
    // req.ResData("获取权限列表")
    // 接收参数
    let type = req.params.type;
    // console.log(type);
    // 判断参数类型
    if (!type || type != "list" && type != "tree")
        return res.ResData("参数错误")
            // 操作数据库
    var sql = "select e.ps_id,ps_name,e.ps_pid,e.ps_level,d.ps_api_path from sp_permission e left join sp_permission_api d on (e.ps_id=d.ps_id)"
    db.query(sql, (err, results) => {
        if (err) res.ResData("获取列表失败")
            // console.log(results);
        if (type == 'list') {
            var arr = []
            for (idx in results) {
                let result = results[idx];
                arr.push({
                    "id": result.ps_id,
                    "authName": result.ps_name,
                    "level": result.ps_level,
                    "pid": result.ps_pis,
                    "path": result.ps_api_path,
                })
            }
            res.ResData("获取权限列表成功", 200, arr)
        } else {
            // // 生成一级菜单
            // let arr = []
            // for (idx in results) {
            //     if (results[idx].ps_level == 0) {
            //         arr.push({
            //             id: results[idx].ps_id,
            //             authName: results[idx].ps_name,
            //             path: results[idx].ps_api_path,
            //             pid: results[idx].ps_pid,
            //             children: []
            //         })
            //     }
            // }
            // // console.log(arr);
            // // 生成二级菜单
            // for (idx in results) {
            //     if (results[idx].ps_level == 1) {
            //         for (var j = 0; j < arr.length; j++) {
            //             if (results[idx]['ps_pid'] == arr[j].id) {
            //                 arr[j].children.push({
            //                     id: results[idx].ps_id,
            //                     authName: results[idx].ps_name,
            //                     path: results[idx].ps_api_path,
            //                     pid: results[idx].ps_pid,
            //                     children: []
            //                 })
            //             }
            //         }
            //     }
            // }
            // // res.ResData(arr);
            // // 生成三级菜单
            // for (idx in results) {
            //     if (results[idx].ps_level == 2) {
            //         for (var j = 0; j < arr.length; j++) { //找一级菜单
            //             var secondMenu = arr[j].children //二级菜单
            //             for (var i = 0; i < secondMenu.length; i++) { //找出二级菜单里的每一项
            //                 if (results[idx]['ps_pid'] == secondMenu[i].id) {
            //                     secondMenu[i].children.push({
            //                         id: results[idx].ps_id,
            //                         authName: results[idx].ps_name,
            //                         path: results[idx].ps_api_path,
            //                         pid: results[idx].ps_pid + ',' + arr[j].id,
            //                     })
            //                 }
            //             }
            //         }
            //     }
            // }
            // res.ResData(arr);
            getTree(results, [])
            res.ResData(arr);

        }
    })
}



// 角色列表
exports.rolesList = async(req, res) => {
    // res.ResData("角色列表")
    // 角色查询列表
    var sql1 = "select * from sp_role";
    var sql2 = "select e.ps_id,ps_name,e.ps_pid,e.ps_level,d.ps_api_path from sp_permission e left join sp_permission_api d on (e.ps_id=d.ps_id)"
    var result1 = await query(sql1);
    var result2 = await query(sql2);
    let arr = [];
    for (idx in result1) {
        let keyId = result1[idx].ps_ids.split(",");
        var tree = getTree(result2, keyId);
        // console.log(keyId);
        // 获取一级菜单
        // let arr1 = [];
        // for (i in result2) {
        //     if (result2[i].ps_level == 0) {
        //         // console.log(result2[idx].ps_level);
        //         var ps_id = result2[i].ps_id + '';
        //         if (keyId.length !== 0 && keyId.indexOf(ps_id) == -1) {
        //             continue;
        //         }
        //         arr1.push({
        //             id: result2[i].ps_id,
        //             authName: result2[i].ps_name,
        //             pid: result2[i].ps_pid,
        //             path: result2[i].ps_api_path,
        //             children: []
        //         })
        //     }
        // }
        // // 获取二级菜单
        // for (i in result2) {
        //     if (result2[i].ps_level == 1) {
        //         for (var j = 0; j < arr1.length; j++) {
        //             if (result2[idx]['ps_pid'] == arr[j].id) {
        //                 if (keyId.length !== 0 && keyId.indexOf(ps_id) == -1) {
        //                     continue;
        //                 }
        //                 arr[j].children.push({
        //                     id: result2[i].ps_id,
        //                     authName: result2[i].ps_name,
        //                     path: result2[i].ps_api_path,
        //                     pid: result2[i].ps_pid,
        //                     children: []
        //                 })
        //             }
        //         }
        //     }
        // }
        // console.log(keyId);
        let obj = {
            "id": result1[idx].ps_id,
            "roleName": result1[idx].ps_name,
            "roleDesc": result1[idx].ps_desc,
            "children": tree,
        }
        arr.push(obj)
    }
    res.ResData(arr)
}